<?php
require("config.php");

//start session
session_start();

if(!isset($_SESSION["customer"])) {
	header("location: account.php");
	exit();
} 
//display shopping cart items in a table (non editable) - check
//confirm button &  edit shopping cart button - check
//display customer details on checkout page - check
//if confirm, get billing information, click continue, then save to db
//go to report page (create pdf of receipt)
//automatically redirect to index page
//save purchase history on user account page (get from db using cust_id)
?>
<?php
//display shopping cart items in a table
$cartOutput ="";
$cartTotal = "";
$cartArticles = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
	$cartOutput = "<h2 align='center'>Cannot Check Out Items. Your Shopping Cart is Empty</h2><br />
		<div align='center'><form action=gallery.php method='post'><input name='backBtn' type='submit' value='Back to Gallery'/><form><br /><br/></div>";
	$cartTotal = "Total: $0.00";
	$cartArticles = 0;
} else {
	$i = 0;
	foreach($_SESSION["cart_array"] as $each_item){
		$item_id = $each_item['item_id'];//variable to access the database for that item
		$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
		//access each variable from database
		$cartArticles = $each_item['quantity'] + $cartArticles;
		while($row = mysql_fetch_array($sql)) {
			$product_name = $row["product_name"];
			$price = $row["price"];
			$details = $row["details"];
		}	
		$priceTotal = $price * $each_item['quantity'];
		$cartTotal = $priceTotal + $cartTotal;
		
		setlocale(LC_MONETARY, "en_US");
		$priceTotal = money_format("%10.2n", $priceTotal);
		//dynamic table row assembly
		$cartOutput .= "<tr align='center'>";
		$cartOutput .= '<td>'.$product_name.'<br /><img style="margin:5px;" src="inventory_images/'.$item_id.'.jpg" alt="'.$product_name.'" width="50" height="62" border="1"/></td>';
		$cartOutput .= "<td>".$details."</td>";
		$cartOutput .= "<td>$".$price."</td>";
		$cartOutput .= "<td>".$each_item['quantity']."</td>";
		$cartOutput .= "<td>".$priceTotal."</td>";
		$cartOutput .= "</tr>";
		$i++;
	}
	setlocale(LC_MONETARY, "en_US");
	$cartTotal = money_format("%10.2n", $cartTotal);
	$cartTotal = "Total : ".$cartTotal;
}
?>
<?php
//display customer information on checkout page
if(isset($_SESSION["customer"]) && isset($_SESSION["cart_array"])) {
	$customerDetails = "";
	$customer = $_SESSION["customer"];
	$sql = mysql_query("SELECT * FROM customers WHERE u_name='$customer';");
	$count = mysql_num_rows($sql);
	
	if ($count == 1) {
		while ($row = mysql_fetch_array($sql)) {
			$id = $row["id"];
			$customer_name = $row["f_name"]."\n".$row["l_name"];
			$address = $row["address"];
			$email = $row["email"];
		}
		$customerDetails .= '<tr><td><strong>Customer Name: </strong>'.$customer_name.'</td></tr>';
		$customerDetails .= '<tr><td><strong>Address: </strong>'.$address.'</td></tr>';
		$customerDetails .= '<tr><td><strong>Email: </strong>'.$email.'</td></tr>';
	}
	
}
?>